package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.hongxun.design.R;
import com.hongxun.design.bean.SettingItemBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.ui.switchview.SwitchView;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/12.
 */
public class AppSettingsAdapter extends BaseAdapter {
    Context context;
    List<SettingItemBean> itemBeans;
    LayoutInflater inflater;

    public AppSettingsAdapter(Context context, List<SettingItemBean> itemBeans) {
        this.context = context;
        this.itemBeans = itemBeans;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return itemBeans.get(position).getType();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        SettingItemBean bean = (SettingItemBean) getItem(i);
        int viewType = getItemViewType(i);
        Hollder hollder = null;
        PushHollder pushHollder = null;

        if (view == null) {
            switch (viewType) {
                case Constant.setting_value:
                    view = inflater.inflate(R.layout.listview_item_appsetvalue, null);
                    hollder = new Hollder(view);
                    view.setTag(hollder);
                    break;
                case Constant.setting_push:
                    view = inflater.inflate(R.layout.listview_item_appsetpush, null);
                    pushHollder = new PushHollder(view);
                    view.setTag(pushHollder);
                    break;
            }
        } else {
            switch (viewType) {
                case Constant.setting_value:
                    hollder = (Hollder) view.getTag();
                    break;
                case Constant.setting_push:
                    pushHollder = (PushHollder) view.getTag();
                    break;
            }
        }

        try {
            switch (viewType) {
                case Constant.setting_value:
                    hollder.title.setText(bean.getTitle());
                    hollder.value.setText(bean.getValue());
                    hollder.item.setOnClickListener(bean.getListener());
                    break;
                case Constant.setting_push:
                    pushHollder.title.setText(bean.getTitle());
                    pushHollder.push.setOpened(bean.isInitflag());
                    pushHollder.push.setOnStateChangedListener(bean.getSlistener());
                    break;
            }
        } catch (Exception e) {
        }
        return view;
    }


    class Hollder {
        TextView title, value, right;
        RelativeLayout item;
        Hollder(View v) {
            title = (TextView) v.findViewById(R.id.appsetvalue_title);
            value = (TextView) v.findViewById(R.id.appsetvalue_value);
            right = (TextView) v.findViewById(R.id.appsetvalue_right);
            item = (RelativeLayout) v.findViewById(R.id.appsetvalue_item);
            SetTypeface.set(right, IconSourse.RIGHTARROW);
        }
    }

    class PushHollder {
        TextView title;
        SwitchView push;

        PushHollder(View v) {
            title = (TextView) v.findViewById(R.id.appsetvalue_title);
            push = (SwitchView) v.findViewById(R.id.appsetvalue_push);
            push.setCheckedcolor(R.color.login_yellow);
        }
    }
}
